package com.ycsj.goldmedalnewconcept.master.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ycsj.common.adapter.VpCommonAdapter;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.view.vp.NoScrollViewPager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.master.bean.TeacherManagerInfo;
import com.ycsj.goldmedalnewconcept.master.fragment.MasterBaseMessageFragment;
import com.ycsj.goldmedalnewconcept.master.fragment.MasterClassMessageFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 教师详情
 * Created by Administrator on 2018/7/16 0016.
 */
public class MasterTeacherInfoActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {


    //返回
    private ImageView ivBack;

    //学生
    private ImageView lvStudent;

    //班级
    private ImageView lvClass;

    //标题
    private TextView tv_st_title;

    //不可滚动ViewPager
    private NoScrollViewPager vp;

    //填充ViewPager的页面
    private List<Fragment> mFragments;

    //ViewPager的适配器
    private VpCommonAdapter vpAdapter;

    //传递的学生对象
    private TeacherManagerInfo.ListBean info;

    //Tab页面选中的资源图片集合
    private int[] tabSelectIds = {R.drawable.class_data_select, R.drawable.class_msg_select};

    //Tab页面未选中的资源图片集合
    private int[] tabUnselectIds = {R.drawable.class_data_normal, R.drawable.class_msg_normal};


    @Override
    protected int getContentLayout() {
        return R.layout.master_activity_teacherinfo;
    }

    @Override
    protected void init() {

        initStateBar();

        //获取班级号
        initInent();

        //初始化视图
        initView();

        //设置适配器
        initVp();
    }


    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        lvStudent = findViewById(R.id.lv_student);
        lvStudent.setOnClickListener(this);
        lvClass = findViewById(R.id.iv_class);
        lvClass.setOnClickListener(this);
        vp = findViewById(R.id.vp);
//        vp.setPagerEnabled(false);
        tv_st_title = findViewById(R.id.tv_title);
        if (null != info) {
            tv_st_title.setText(info.getName());
        }
    }

    /**
     * 获取学生对象信息
     */
    private void initInent() {
        info = (TeacherManagerInfo.ListBean) getIntent().getSerializableExtra("data");
    }

    /**
     * 初始化VieWPager
     */
    private void initVp() {
        mFragments = new ArrayList<>();
        mFragments.add(MasterBaseMessageFragment.newInstance(info.getUserid() + "",  "2"));
        mFragments.add(MasterClassMessageFragment.newInstance(info.getUserid() + "",  "2"));
        vpAdapter = new VpCommonAdapter(getSupportFragmentManager(), mFragments);
        vp.setOffscreenPageLimit(mFragments.size());
        vp.setAdapter(vpAdapter);
        vp.addOnPageChangeListener(this);
    }


    /**
     * 同步ViewPager页面和切换选中Tab的方法
     *
     * @param pos
     */
    private void syncVpState(int pos) {
        vp.setCurrentItem(pos);
        if (pos == 0) {
            lvStudent.setImageResource(tabSelectIds[0]);
            lvClass.setImageResource(tabUnselectIds[1]);
        } else if (pos == 1) {
            lvStudent.setImageResource(tabUnselectIds[0]);
            lvClass.setImageResource(tabSelectIds[1]);
        }
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.lv_student) {
            //学生
            syncVpState(0);
        } else if (id == R.id.iv_class) {
            //班级
            syncVpState(1);
        }
    }

    /**
     * 选中监听
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            lvStudent.setImageResource(tabSelectIds[0]);
            lvClass.setImageResource(tabUnselectIds[1]);
        } else if (position == 1) {
            lvStudent.setImageResource(tabUnselectIds[0]);
            lvClass.setImageResource(tabSelectIds[1]);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
